<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<head>
<title><spring:message code="label.userList" /></title>
<style>
</style>


</head>
<sec:authorize access="hasAnyRole('ROLE_USER', 'ROLE_ADMIN')">
	<tiles:insertDefinition name="defaultTemplate">
		<tiles:putAttribute name="body">
			<div class="body">
				<div class="col-sm-10 col-md-10 ">
					<div class="panel panel-primary">
						<div class="panel-heading">
							<h3 class="panel-title">
								<strong><spring:message code="label.userList" /></strong>
								<button class="btn btn-lg btn-primary" id="btnAddUser"
									type="button" data-toggle="modal" data-target="#myModal"
									value="submit" name="submit" title="Create new user">+</button>
							</h3>
						</div>

						<!--create form  -->
					
					
						
						<c:if test="${not empty mess}">
							
						<c:set var="message" scope="request"><spring:message code="label.userAdded" /></c:set>

							<c:if test="${mess ne message}">
								<div class="alert alert-warning">
									<strong>${mess}</strong>
								</div>
								
							</c:if>						
								<c:if test="${mess eq message}">
								<div class="alert alert-success">
									<strong>${mess}</strong>
								</div>
								
							</c:if>

						</c:if>


						<!-- pop up confirm -->
						<div class="modal fade" id="myModal" tabindex="-1" role="dialog"
							aria-labelledby="myModalLabel" aria-hidden="true">
							<div class="modal-dialog">
								<div class="modal-content">
									<div class="modal-header">

										<button type="button" id="btnClose" class="close"
											data-dismiss="modal">
											<span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
										</button>

										<h4 class="modal-title" id="myModalLabel"><spring:message code="label.userListTitle"/></h4>

									</div>
									<div class="modal-body" >
										<form action="createNewUser" method="POST">
											<!-- datatable -->
											<table  class="table table-hover ">
												<tbody>
													<tr>
														<td style="white-space: nowrap;"><strong><spring:message
																	code="label.userName" /></strong></td>
														<td style="white-space: nowrap;"><input value="" type="text"
															name="txtUsername" id="txtUsername" class="form-control"
															required="required" maxlength="6" autocomplete="off"><span
															style="color: green" id="messageCheck"></span></td>



													</tr>
													<tr>
														<td style="white-space: nowrap;"><strong><spring:message
																	code="label.passWord" /></strong></td>
														<td style="white-space: nowrap;"><input
															type="password" name="txtPassword" id="txtPassword"
															class="form-control" required="required"
															autocomplete="off" ></td>
													</tr>
													<tr>
														<td style="white-space: nowrap;"><strong><spring:message
																	code="label.role" /></strong></td>
														<td style="white-space: nowrap;"><label><input
																name="cbAdmin" id="cbAdmin" type="checkbox"
																value="ROLE_ADMIN"> <spring:message
																	code="label.adminRole" /></label></td>
													</tr>
													<tr>
														<td></td>
														<td align="right">
															<button class="btn btn-lg btn-primary" type="button"
																id="btnClear">
																<spring:message code="label.clear" />
															</button>
															<button class="btn btn-lg btn-primary" type="submit"
																value="submit" name="submit" id="btnSubmit"
																onclick="formhash(this,this.form.txtPassword);">
																<spring:message code="label.create" />
															</button>
														</td>
													</tr>
												</tbody>
											</table>
										</form>
									</div>
								</div>
							</div>
						</div>


<div class="table-responsive">
						<table id="tblUserList"
							class="table table-striped table-bordered table-hover">
							<thead>
								<tr>
									<td>1</td>
									<td style="white-space: nowrap;"><strong><spring:message
												code="label.userName" /></strong></td>
									<td style="white-space: nowrap;"><spring:message
											code="label.role" /></td>
								</tr>
							</thead>
							<tbody>


								<c:forEach var="listRole" items="${listRole}">
									<tr>
										<td>1</td>
										<td>${listRole.user.username}</td>
										<td>${listRole.role}</td>
									</tr>
								</c:forEach>


							</tbody>
						</table></div>

					</div>
				</div>
			</div>
			<!--//body  -->
		</tiles:putAttribute>
	</tiles:insertDefinition>
</sec:authorize>

<!-- javascript -->
<script src="resources/js/sha1.js" type="text/javascript"></script>
<script type="text/javascript">

function formhash(form, txtPassword) {
	txtPassword.value = SHA1(txtPassword.value);
}

	//active panel
	$("#userList").addClass('active1'); // activated list-item

	$("#advanceSearch").hide();

	$('#tblUserList').dataTable({responsive : true});

	$("#btnClose ,#btnClear").click(function() {
		$('#txtUsername').val("");
		$('#txtPassword').val("");
		$('#cbAdmin').attr('checked', false); // Unchecks it
		$('#messageCheck').html("");
	})

	$("#txtUsername").keyup(function ajaxChecking() {
		$('#messageCheck').html('<spring:message javaScriptEscape="true" code="label.checking" />');	
	})
	

	$("#txtUsername").change(function ajaxCheckUsername() {
		if ($('#messageCheck').val()=="") {
			$('#messageCheck').html("");
		}
		
		
		
		$.ajax({
			url : 'usernameCheck.html',
			data : ({
				name : $("#txtUsername").val()
			}),
			success : function(data) {
				$('#messageCheck').html(data);

				if (data=='<spring:message code="label.nameTaken"/>') {
					$('#btnSubmit').prop("disabled",true);
				}else{$('#btnSubmit').prop("disabled",false);}

			}
		});
	})
</script>
